package cn.hhchat.wp.service;

import cn.hhchat.wp.dto.form.RegisterForm;
import cn.hhchat.wp.dto.form.UserInfoEditForm;
import cn.hhchat.wp.entity.JoinRelationEntity;
import cn.hhchat.wp.entity.GroupEntity;
import cn.hhchat.wp.entity.RoleEntity;
import cn.hhchat.wp.entity.UserEntity;
import cn.hhchat.wp.enums.GradeEnum;
import cn.hhchat.wp.enums.RoleEnum;
import cn.hhchat.wp.exception.DupUserRegException;
import cn.hhchat.wp.repository.JoinRelationRepository;
import cn.hhchat.wp.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created this one by HMH on 2017/10/22.
 */
@Service
@Transactional
public class UserService {

    public UserEntity getByUsername(String username) {
        return userRepository.findByUsername(username);
    }

    public int getUserCount() {
        return userRepository.countAllBy();
    }

    public UserEntity getByEmail(String email){
        return userRepository.findByEmail(email);
    }

    public UserEntity getById(long id) {
        return userRepository.findOne(id);
    }

    public List<UserEntity> getAll() {
        return userRepository.findAll();
    }

    public UserEntity save(UserEntity userEntity) {
        return userRepository.save(userEntity);
    }

    /**
     * 创建用户
     * @param form 注册表单
     * @return 存储 -> 填充id -> 返回用户对象
     */
    public UserEntity createUser(RegisterForm form) {
//        检查重复注册
        if (null != userRepository.findByEmailOrUsername(form.getEmail(), form.getUsername())) {
            throw new DupUserRegException("邮箱或用户名已经被重复注册");
        }
//        生成用户
        UserEntity userEntity = new UserEntity(form);

//        设置基础权限
//        如果是第一个注册的，给予系统管理员权限
        if (getUserCount() == 0) {
            roleService.setRole(userEntity, RoleEnum.系统管理员);
            roleService.setRole(userEntity, RoleEnum.管理员);
        }
        if(userEntity.getGrade()== GradeEnum.教师){
            roleService.setRole(userEntity, RoleEnum.教师);
        } else{
            roleService.setRole(userEntity, RoleEnum.学生);
        }
        roleService.setRole(userEntity, RoleEnum.用户);

//        否则是普通用户
//        存储并返回
        return save(userEntity);
    }

    public int countGroupMembers(GroupEntity group) {
        return joinRelationRepository.countByGroupAndIsActive(group, true);
    }

    public List<UserEntity> getGroupMembers(GroupEntity group) {
        List<JoinRelationEntity> activeJoinList = joinRelationRepository.findByGroupAndIsActive(group, true);
        return activeJoinList.stream().map(JoinRelationEntity::getUser).collect(Collectors.toList());
    }

    public UserEntity editUser(UserEntity user, UserInfoEditForm userInfoEditForm) {
        user = user.edit(userInfoEditForm);
        return save(user);
    }

    public List<UserEntity> getAllUserInRole(RoleEntity role) {
        return userRepository.findByRolesContains(role);
    }

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private JoinRelationRepository joinRelationRepository;

    @Autowired
    private RoleService roleService;
}
